<?php
declare (strict_types=1);

namespace app\course\service\base\admin;

use app\common\logic\category\admin\Category;
use app\common\service\BaseService;
use app\course\logic\base\admin\Course as CourseLogic;
use app\course\logic\classes\admin\Classes as ClassesLogic;
use app\course\logic\live\admin\Live as LiveLogic;
use app\course\logic\live\admin\LiveSmall as LiveSmallLogic;
use app\course\logic\video\admin\Video as VideoLogic;
use eduline\admin\page\PageList;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Request;
use think\Response;

class Course extends BaseService
{

    /**
     * 获取课程列表
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-14
     *
     * @return Response [type]                         [description]
     * @throws DbException
     */
    public function getCourseList(): Response
    {
        $mhmId = Request::get('mhm_id/d', request()->mhm_id);
        $map   = [
            // ['mhm_id', '=', $mhm_id],
        ];
        // 条件
        Request::has('title', 'get', true) && $map[] = ['title', 'like', '%' . Request::get('title/s') . '%'];
        $category = Request::get('category/d', 0);
        if ($category && $mhmId === 1) {
            $map[] = ['category', 'like', '%,' . $category . ',%'];
            $map[] = ['mhm_id', '=', $mhmId];
        } else if ($category) {
            $map[] = ['mhm_category', 'like', '%,' . $category . ',%'];
        }
        // 是否过滤免费课程
        Request::has('filter_free', 'get') && $map[] = ['price', '>', 0];
        // 数量及类型
        $count = Request::get('count/d', 15);
        $type  = Request::get('type/d', 1);

        $field = ['id', 'title', 'cover', 'scribing_price', 'price', 'course_type'];

        switch ($type) {
            case 2:
                $data = app(LiveLogic::class)->with(['cover_url'])->where($map)->field($field)->paginate($count);
                break;
            case 3:
                $data = app(LiveSmallLogic::class)->with(['cover_url'])->where($map)->field($field)->paginate($count);
                break;
            case 4:
                $data = app(ClassesLogic::class)->with(['cover_url'])->where($map)->field($field)->paginate($count);
                break;
            case 1:
            default:
                $data = app(VideoLogic::class)->with(['cover_url'])->where($map)->field($field)->paginate($count);
                break;
        }

        // 设置表单
        $list        = app(PageList::class);
        $list->datas = $data;
        $list->types = [
            ['id' => 1, 'title' => '点播课'],
            ['id' => 2, 'title' => '大班课'],
            ['id' => 3, 'title' => '小班课'],
            ['id' => 4, 'title' => '班级课'],
        ];

        return $list->send();

    }

    /**
     * 获取课程分类列表
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-18
     *
     * @param int $pid [description]
     * @return Response [type]                              [description]
     */
    public function getCourseCategory(int $pid): Response
    {

        $cates = app(Category::class)->getList($pid)->visible(['id', 'title', 'hasChildren'])->toArray();

        return $this->parseToData($cates, 1, '获取成功');
    }

    /**
     * 复制课程信息
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/9/2
     *
     * @param int $courseId
     * @return Response
     */
    public function copyCourseInfo(int $courseId): Response
    {
        app(CourseLogic::class)->copyCourseInfo($courseId);

        return $this->parseToData([], 1, '操作成功');
    }

    /**
     * 批量设置课程字段值
     *
     * @param string $field
     * @return Response
     */
    public function batchUpdatedField(string $field): Response
    {
        $value = Request::put($field);

        /** @var  CourseLogic $courseLogic */
        $courseLogic = app(CourseLogic::class);
        $courseLogic->batchUpdatedField($field, $value);

        return $this->parseToData([], 1, '操作成功');
    }

    /**
     * 批量获取课程信息
     *
     * @return Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getCourses(): Response
    {
        /** @var array $ids */
        $ids = Request::get('ids/a');

        /** @var  CourseLogic $courseLogic */
        $courseLogic = app(CourseLogic::class);

        $datas = $courseLogic->getCourses($ids);

        return $this->parseToData($datas, 1, '获取成功');
    }

    /**
     * 批量取消推荐课程
     *
     * @return Response
     */
    public function cancelRecommend()
    {
        /** @var array $ids */
        $ids = Request::put('ids/a');

        /** @var  CourseLogic $courseLogic */
        $courseLogic = app(CourseLogic::class);

        $courseLogic->batchUpdated($ids, 'is_recommend', 0);

        return $this->parseToData([], 1, '操作成功');
    }
}
